#!/bin/sh
#
# Resource script for MailTo
#
# Author: Alan Robertson <alanr@unix.sh>
#
# Description: sends email to a sysadmin whenever a takeover occurs.
#
#	Note: This command requires an argument, unlike normal init scripts.
#
#	This can be given in the haresources file as:
#
#	You can also give a mail subject line or even multiple addresses
#		MailTo::alanr@unix.sh::BigImportantWebServer
#		MailTo::alanr@unix.sh,spoppi@gmx.de::BigImportantWebServer
#
#	This will then be put into the message subject and body.
#
#	  OCF parameters are as below:
#		OCF_RESKEY_email
#		OCF_RESKEY_subject
#
# License:  GNU General Public License (GPL)
#
# Copyright:	(C) 2005 International Business Machines

#######################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

# Parameter defaults

OCF_RESKEY_email_default=""
OCF_RESKEY_subject_default="Resource Group"

: ${OCF_RESKEY_email=${OCF_RESKEY_email_default}}
: ${OCF_RESKEY_subject=${OCF_RESKEY_subject_default}}

#######################################################################

ARGS="$0 $*"

us=`uname -n`

usage() {
  echo "Usage: $0 {start|stop|status|monitor|meta-data|validate-all}"
}

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="MailTo">
<version>1.0</version>

<longdesc lang="en">
This is a resource agent for MailTo. It sends email to a sysadmin whenever 
a takeover occurs.
</longdesc>
<shortdesc lang="en">Notifies recipients by email in the event of resource takeover</shortdesc>

<parameters>
<parameter name="email" unique="0" required="1">
<longdesc lang="en">
The email address of sysadmin.
</longdesc>
<shortdesc lang="en">Email address</shortdesc>
<content type="string" default="$[OCF_RESKEY_email_default]" />
</parameter>

<parameter name="subject" unique="0">
<longdesc lang="en">
The subject of the email.
</longdesc>
<shortdesc lang="en">Subject</shortdesc>
<content type="string" default="${OCF_RESKEY_subject_default}" />
</parameter>
</parameters>

<actions>
<action name="start" timeout="10s" />
<action name="stop" timeout="10s" />
<action name="status" depth="0" timeout="10s" interval="10s" />
<action name="monitor" depth="0" timeout="10s" interval="10s" />
<action name="meta-data" timeout="5s" />
<action name="validate-all" timeout="5s" />
</actions>
</resource-agent>
END
}

MailProgram() {
	$MAILCMD -s "$1" "$email"  <<EOF
        $Subject
 
        Command line was:
        $ARGS
EOF
	return $?
}

SubjectLine() {
  case $1 in
    ??*)	echo "$@";;
    *)		echo "${OCF_RESKEY_subject_default}";;
  esac
}


MailToStart() {

	Subject="`SubjectLine $subject` Takeover in progress at `date` on $us"

	MailProgram "$Subject" $1
	ha_pseudo_resource MailTo_${OCF_RESOURCE_INSTANCE} start
}

MailToStop () {
	Subject="`SubjectLine $subject` Migrating resource away at `date` from $us"

	MailProgram "$Subject" $1
	ha_pseudo_resource MailTo_${OCF_RESOURCE_INSTANCE} stop
}

MailToStatus () {
#	ocf_log warn "Don't stat/monitor me! MailTo is a pseudo resource agent, so the status reported may be incorrect"

	if ha_pseudo_resource MailTo_${OCF_RESOURCE_INSTANCE} monitor
	then
		echo "running"
		return $OCF_SUCCESS
	else
		echo "stopped"
		return $OCF_NOT_RUNNING
	fi
}

MailToValidateAll () {
	if [ -z "$MAILCMD" ]; then
		ocf_exit_reason "MAILCMD not set: complain to the packager"
		exit $OCF_ERR_INSTALLED
	fi
	check_binary "$MAILCMD"

	return $OCF_SUCCESS
}

# 
# See how we were called.
#
#	The order in which heartbeat provides arguments to resource
#	scripts is broken.  It should be fixed.
#

if
  ( [ $# -ne 1 ] )
then
  usage
  exit $OCF_ERR_GENERIC
fi

case $1 in
  meta-data)		meta_data
			exit $OCF_SUCCESS
			;;
  status|monitor)	MailToStatus
			exit $?
			;;
  usage)		usage
			exit $OCF_SUCCESS
			;;
  *)			;;
esac

if 
  [ -z "$OCF_RESKEY_email" ]
then
  ocf_exit_reason "At least 1 Email address has to be given!"
  exit $OCF_ERR_CONFIGURED
fi

email=$OCF_RESKEY_email
subject=$OCF_RESKEY_subject

MailToValidateAll

case $1 in
  start)		MailToStart
			;;
  stop)			MailToStop
			;;
  validate-all)		;;
  *)			usage
			exit $OCF_ERR_UNIMPLEMENTED
			;;
esac
exit $?
